<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="短剧标题" prop="title">
                    <el-input class="w-[280px]" clearable v-model="queryParams.title" />
                </el-form-item>
                <el-form-item label="是否完结" prop="is_completed">
                    <el-select
                        v-model="queryParams.is_completed"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="全部" value="-1" />
                        <el-option label="未完结" value="0" />
                        <el-option label="已完结" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item label="是否允许观看" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="全部" value="-1" />
                        <el-option label="不允许观看" value="0" />
                        <el-option label="允许观看" value="1" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['client:drama:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="短剧编号" show-overflow-tooltip prop="drama_id" min-width="100" />
                <el-table-column label="短剧标题" show-overflow-tooltip prop="title" min-width="100" />
                <el-table-column label="详细描述" show-overflow-tooltip prop="description" min-width="100" />
                <el-table-column label="封面" min-width="100">
                    <template #default="{ row }">
                        <el-avatar :size="50" :src="row.cover_url"></el-avatar>
                    </template>
                </el-table-column>
                <el-table-column label="总集数" prop="total_episodes" min-width="100" />
                <el-table-column label="价格" prop="money" min-width="100" />
                <el-table-column label="创建时间" prop="create_time" min-width="100" />
                <el-table-column label="更新时间" prop="update_time" min-width="100" />
                <el-table-column label="操作" width="190" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['client:drama:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button v-perms="['client:series:list']" type="primary" link>
                            <router-link :to="{
                                        path: getRoutePath('client:series:list'),
                                        query: {
                                            drama_id: row.drama_id,
                                            drama_title: row.title,
                                        }
                                    }">
                                剧集管理
                            </router-link>
                        </el-button>
                        <el-button
                            v-perms="['client:drama:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.drama_id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="drama">
import { dramaDelete, dramaLists } from '@/api/client/drama'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { getRoutePath } from '@/router'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    title: '',
    is_completed: '-1',
    status: '-1',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: dramaLists,
    params: queryParams
})


const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (drama_id: number) => {
    await feedback.confirm('确定要删除？')
    await dramaDelete({ drama_id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
